<?php
namespace app\index\model;

use think\Model;
use think\facade\Db;
use think\facade\Cache;
use think\model\concern\SoftDelete;

class Cate extends Model
{
    use SoftDelete;
    protected $deleteTime = 'delete_time';
    //如果表名和文件名不是对应的，用下面代码修改
	//cate_main 这个作为最重要的一个标签，返回包括他的标题和id
	public function cate_main($id, $nums,  $order, $field, $where)
	{
		$main=Db::name('cate')->where('id',$id)->whereNull('delete_time')->where('isopen',1)->field('id,catename,pic,en_name')->find();
		if ($order) {
			$order='is_tui desc,create_time desc,id asc';
		} else {
			$order='is_tui desc,create_time asc,id desc';
		}
		$ids=(new \app\index\model\Cate())->where('fid', $id)->where('type', 'in', [1,2,3])->column('id');
		$ids[]=$id;
		if (env('app_debug',false)) {
			$zi=[];
			$offset=0;
			foreach ($nums as $num){
				$c=(new \app\index\model\Article())->where('isopen', 1)->where('cateid','in', $ids)->where($where)->field($field)->limit($offset, $num)->order($order)->select()->toArray();
				foreach ($c as $k=>$v) {
					if (!$v['pic']) {
						$c[$k]['pic']=(new \app\index\model\ArticleImg())->where('aid', $v['id'])->value('pic');
					}
				}
				$zi[]=$c;
				$offset=$offset+$num;
			}
		} else {
			if (!$zi=Cache::get('cate_main'.$id.implode("_", $nums))) {
				$zi=[];
				$offset=0;
				foreach ($nums as $num){
					$c=(new \app\index\model\Article())->where('isopen', 1)->where('cateid', 'in', $ids)->where($where)->field($field)->limit($offset, $num)->order($order)->select()->toArray();
					foreach ($c as $k=>$v) {
						if (!$v['pic']) {
							$c[$k]['pic']=(new \app\index\model\ArticleImg())->where('aid', $v['id'])->value('pic');
						}
					}
					$zi[]=$c;
					$offset=$offset+$num;
				}
				Cache::set('cate'.$id.implode("_", $nums), $zi, 3600);
			}
		}
		$main['zi']=$zi;
		return [$main];
	}
    public function cate($id, $num, $offset, $order, $field, $where)
    {
        if ($id) {
            $ids=(new \app\index\model\Cate())->where('fid', $id)->where('type', 'in', [1,2,3])->column('id');
            $ids[]=$id;
        } else {
            $ids=(new \app\index\model\Cate())->where('type', 'in', [1,2,3])->column('id');
        }
        if ($order) {
            $order='is_tui desc,create_time desc,id asc';
        } else {
            $order='is_tui desc,create_time asc,id desc';
        }
        if (env('app_debug',false)) {
            $cate=(new \app\index\model\Article())->where('isopen', 1)->where('cateid', 'in', $ids)->where($where)->field($field)->limit($offset, $num)->order($order)->select()->toArray();
            foreach ($cate as $k=>$v) {
                if (!$v['pic']) {
                    $cate[$k]['pic']=(new \app\index\model\ArticleImg())->where('aid', $v['id'])->value('pic');
                }
            }
        } else {
            if (!$cate=Cache::get('cate'.$id)) {
                $cate=(new \app\index\model\Article())->where('isopen', 1)->where('cateid', 'in', $ids)->where($where)->field($field)->limit($offset, $num)->order($order)->select()->toArray();
                foreach ($cate as $k=>$v) {
                    if (!$v['pic']) {
                        $cate[$k]['pic']=(new \app\index\model\ArticleImg())->where('aid', $v['id'])->value('pic');
                    }
                }
                Cache::set('cate'.$id, $cate, 3600);
            }
        }
        return $cate;
    }
    public function cates($ids, $num, $order, $field, $where)
    {
        if (!is_array($ids) or empty($ids)) {
            return 'ids 必须为数组！';
        }
        if ($order) {
            $order1='id asc';
        } else {
            $order1='id desc';
        }
        $id=implode("", $ids);

        if (env('app_debug',false)) {
            $cates=(new \app\index\model\Cate())->where('fid', 0)->where('isopen', 1)->where('id', 'in', $ids)->where('type', 'in', [1,2,3])->order($order1)->field('id,catename,en_name')->select()->toArray();

            foreach ($cates as $k=>$v) {
                if ($order) {
                    $order='is_tui desc,create_time desc,id asc';
                } else {
                    $order='is_tui desc,create_time desc,id desc';
                }
                $data=(new \app\index\model\Article())->where('isopen', 1)->where('cateid', $v['id'])->where($where)->field($field)->limit($num)->order($order)->select()->toArray();
                foreach ($data as $k1=>$v1) {
                    if (!$v1['pic']) {
                        $data['pic']=(new \app\index\model\ArticleImg())->where('aid', $v1['id'])->value('pic');
                    }
                }
                $cates[$k]['zi']=$data;
            }

        } else {
            if (!$cates=Cache::get('$cates'.$id)) {
                $cates=(new \app\index\model\Cate())->where('fid', 0)->where('isopen', 1)->where('id', 'in', $ids)->where('type', 'in', [1,2,3])->order($order1)->field('id,catename,en_name')->select()->toArray();
                foreach ($cates as $k=>$v) {
                    if ($order) {
                        $order='is_tui desc,create_time desc,id asc';
                    } else {
                        $order='is_tui desc,create_time desc,id desc';
                    }
                    $data=(new \app\index\model\Article())->where('isopen', 1)->where('cateid', $v['id'])->where($where)->field($field)->limit($num)->order($order)->select()->toArray();
                    foreach ($data as $k1=>$v1) {
                        if (!$v1['pic']) {
                            $data['pic']=(new \app\index\model\ArticleImg())->where('aid', $v1['id'])->value('pic');
                        }
                    }
                    $cates[$k]['zi']=$data;
                }
                Cache::set('cates'.$id, $cates, 3600);
            }
        }
        return $cates;
    }
    public function cateall($ids, $nums, $unids, $order, $field, $where)
    {
        if (!is_array($ids) or empty($ids)) {
            return 'ids 必须为数组！';
        }
        if (!is_array($nums) or empty($nums)) {
            return 'num 必须为数组！';
        }
        if ($order) {
            $order1='id asc';
        } else {
            $order1='id desc';
        }
        $id=implode("", $ids);
        if (env('app_debug',false)) {
            $cateall=(new \app\index\model\Cate())->where('fid', 0)->where('isopen', 1)->where('id', 'in', $ids)->where('type', 'in', [1,2,3])->order($order1)->field('id,catename,en_name')->select()->toArray();
            foreach ($cateall as $k=>$v) {
                if ($order) {
                    $order='is_tui desc,create_time desc,id asc';
                } else {
                    $order='is_tui desc,create_time desc,id desc';
                }
                $data=(new \app\index\model\Cate())->where('fid', $v['id'])->where('isopen', 1)->where('id', 'notin', $unids)->where('type', 'in', [1,2,3])->order($order1)->field('id,catename,en_name')->select()->toArray();
                foreach ($data as $k=>$v) {
                    $zi=[];
                    $offset=0;
                    foreach ($nums as $num){
                        $c=(new \app\index\model\Article())->where('isopen', 1)->where('cateid', $v['id'])->where($where)->field($field)->limit($num)->order($order)->select()->toArray();
                        foreach ( $c as $k1=>$v1) {
                            if (!$v1['pic']) {
                                $c[$k1]['pic']=(new \app\index\model\ArticleImg())->where('aid', $v1['id'])->value('pic');
                            }
                        }
                        $zi[]=$c;
                        $offset=$offset+$num;
                    }
                    $data[$k]['zi']=$zi;
                }
                $cateall=$data;
            }
        } else {
            if (!$cateall=Cache::get('$cateall'.$id)) {
                $cateall=(new \app\index\model\Cate())->where('fid', 0)->where('isopen', 1)->where('id', 'in', $ids)->where('type', 'in', [1,2,3])->order($order1)->field('id,catename,en_name')->select()->toArray();
                foreach ($cateall as $k=>$v) {
                    if ($order) {
                        $order='is_tui desc,create_time desc,id asc';
                    } else {
                        $order='is_tui desc,create_time desc,id desc';
                    }
                    $data=(new \app\index\model\Cate())->where('fid', $v['id'])->where('isopen', 1)->where('id', 'notin', $unids)->where('type', 'in', [1,2,3])->order($order1)->field('id,catename,en_name')->select()->toArray();
                    foreach ($data as $k=>$v) {
                        $zi=[];
                        $offset=0;
                        foreach ($nums as $num){
                            $c=(new \app\index\model\Article())->where('isopen', 1)->where('cateid', $v['id'])->where($where)->field($field)->limit($num)->order($order)->select()->toArray();
                            foreach ( $c as $k1=>$v1) {
                                if (!$v1['pic']) {
                                    $c[$k1]['pic']=(new \app\index\model\ArticleImg())->where('aid', $v1['id'])->value('pic');
                                }
                            }
                            $zi[]=$c;
                            $offset=$offset+$num;
                        }
                        $data[$k]['zi']=$zi;
                    }
                    $cateall=$data;
                }
                Cache::set('cateall'.$id, $cateall, 3600);
            }
        }
        return $cateall;
    }
    public function catelist($ids, $num, $order, $field, $where)
    {
        if (!is_array($ids) or empty($ids)) {
            return 'ids 必须为数组！';
        }
        $id=implode("", $ids);
        if ($order) {
            $order1='id asc';
        } else {
            $order1='id desc';
        }
        $catelist=[];
        if (env('app_debug',false)) {
            foreach ($ids as $k => $v) {
                if ($order) {
                    $order='is_tui desc,create_time desc,id asc';
                } else {
                    $order='is_tui desc,create_time desc,id desc';
                }
                $res=(new \app\index\model\Article())->where('isopen', 1)->where('cateid', $v)->where($where)->field($field)->limit($num)->order($order)->select()->toArray();
                foreach ($res as $k1 => $v1) {
                    if (!$v1['pic']) {
                        $res[$k1]['pic']=(new \app\index\model\ArticleImg())->where('aid', $v1['id'])->value('pic');
                    }
                    $res[$k1]['catename']=(new \app\index\model\Cate())->where('id', $v1['cateid'])->value('catename');
                    $res[$k1]['en_name']=(new \app\index\model\Cate())->where('id', $v1['cateid'])->value('en_name');
                }
				$catelist=array_merge($catelist, $res);
            }
        } else {
            if (!$catelist=Cache::get('catelist'.$id)) {
                foreach ($ids as $k => $v) {
                    if ($order) {
                        $order='is_tui desc,create_time desc,id asc';
                    } else {
                        $order='is_tui desc,create_time desc,id desc';
                    }
                    $res=(new \app\index\model\Article())->where('isopen', 1)->where('cateid', $v)->where($where)->field($field)->limit($num)->order($order)->select()->toArray();
                    foreach ($res as $k1 => $v1) {
                        if (!$v1['pic']) {
                            $res[$k1]['pic']=(new \app\index\model\ArticleImg())->where('aid', $v1['id'])->value('pic');
                        }
                        $res[$k1]['catename']=(new \app\index\model\Cate())->where('id', $v1['cateid'])->value('catename');
                        $res[$k1]['en_name']=(new \app\index\model\Cate())->where('id', $v1['cateid'])->value('en_name');
                    }
                    $catelist=[]; 
					$catelist=array_merge($catelist, $res); 
            
                }
                Cache::set('catelist'.$id, $catelist, 3600);
            }
        }
        return $catelist;
    }
    
    public function hot($id, $num, $img, $offset, $order, $field, $where)
    {
        if ($id==0) {
            $where2=true;
        } else {
            $ids=(new \app\index\model\Cate())->where('fid', $id)->column('id');
            $ids[]=$id;
            $where2=[
                'cateid'=>$ids
            ];
        }
        if ($img) {
            $where3=[
                'pic'=>'not null'
            ];
        } else {
            $where3=true;
        }
        
        if ($order) {
            $order='click desc,create_time desc,id asc';
        } else {
            $order='click desc,create_time desc,id desc';
        }

        if (env('app_debug',false)) {
            $hot=(new \app\index\model\Article())->where('isopen', 1)->where($where3)->where($where2)->where($where)->field($field)->limit($offset, $num)->order($order)->select()->toArray();
            foreach ($hot as $k=>$v) {
                if (!$v['pic']) {
                    $hot[$k]['pic']=(new \app\index\model\ArticleImg())->where('aid', $v['id'])->value('pic');
                }
            }
        } else {
            if (!$hot=Cache::get('hot'.$id.(int)$img)) {
                $hot=(new \app\index\model\Article())->where('isopen', 1)->where($where3)->where($where2)->where($where)->field($field)->limit($offset, $num)->order($order)->select()->toArray();
                foreach ($hot as $k=>$v) {
                    if (!$v['pic']) {
                        $hot[$k]['pic']=(new \app\index\model\ArticleImg())->where('aid', $v['id'])->value('pic');
                    }
                }
                Cache::set('hot'.$id.(int)$img, $hot, 3600);
            }
        }
        return $hot;
    }
    public function rec($id, $num, $img, $offset, $order, $field, $where)
    { //推荐
        if ($id==0) {
            $where2=true;
        } else {
            $ids=(new \app\index\model\Cate())->where('fid', $id)->column('id');
            $ids[]=$id;
            $where2=[
                'cateid'=>$ids
            ];
        }
        if ($order) {
            $order='create_time desc,id asc';
        } else {
            $order='create_time desc,id desc';
        }
        if ($img) {
            $where3=[
                'pic'=>'not null'
            ];
        } else {
            $where3=true;
        }
        if (env('app_debug',false)) {
            $rec=(new \app\index\model\Article())->where('isopen', 1)->where($where3)->where($where2)->where('is_tui', 1)->where($where)->field($field)->limit($offset, $num)->order($order)->select()->toArray();
            foreach ($rec as $k=>$v) {
                if (!$v['pic']) {
                    $rec[$k]['pic']=(new \app\index\model\ArticleImg())->where('aid', $v['id'])->value('pic');
                }
            }
        } else {
            if (!$rec=Cache::get('rec'.$id.(int)$img)) {
                $rec=(new \app\index\model\Article())->where('isopen', 1)->where($where3)->where($where2)->where('is_tui', 1)->where($where)->field($field)->limit($offset, $num)->order($order)->select()->toArray();
                foreach ($rec as $k=>$v) {
                    if (!$v['pic']) {
                        $rec[$k]['pic']=(new \app\index\model\ArticleImg())->where('aid', $v['id'])->value('pic');
                    }
                }
                Cache::set('rec'.$id.(int)$img, $rec, 3600);
            }
        }
        return $rec;
    }
    public function sui($id, $num, $img, $field, $where)
    {
        if ($id==0) {
            $where2=true;
        } else {
            $ids=(new \app\index\model\Cate())->where('fid', $id)->column('id');
            $ids[]=$id;
            $where2=[
                'cateid'=>$ids
            ];
        }
        if ($img) {
            $where3=[
                'pic'=>'not null'
            ];
        } else {
            $where3=true;
        }
        if (env('app_debug',false)) {
            $sui=(new \app\index\model\Article())->where('isopen', 1)->where($where3)->where($where2)->where($where)->field($field)->limit($num)->order(true)->select()->toArray();
            foreach ($sui as $k=>$v) {
                if (!$v['pic']) {
                    $sui[$k]['pic']=(new \app\index\model\ArticleImg())->where('aid', $v['id'])->value('pic');
                }
            }
        } else {
            if (!$sui=Cache::get('sui'.$id.(int)$img)) {
                $sui=(new \app\index\model\Article())->where('isopen', 1)->where($where3)->where($where2)->where($where)->field($field)->limit($num)->order(true)->select()->toArray();
                foreach ($sui as $k=>$v) {
                    if (!$v['pic']) {
                        $sui[$k]['pic']=(new \app\index\model\ArticleImg())->where('aid', $v['id'])->value('pic');
                    }
                }
                Cache::set('sui'.$id.(int)$img, $sui, 3600);
            }
        }
        return $sui;
    }
   
   
    public function breadcrumb($controller, $action, $id)
    {
        $cate=[];
        if ($action=='cate') {
            if ($wo=(new \app\index\model\Cate())->where('id', $id)->field('id,fid,catename')->find()) {
                $cate[]=$wo;
            }
            if ($wo=(new \app\index\model\Cate())->where('id', $wo['fid'])->field('id,fid,catename')->find()) {
                $cate[]=$wo;
            }
        }
        if ($action=='show') {
            $id=(new \app\index\model\Article())->where('id', $id)->value('cateid');
            if ($wo=(new \app\index\model\Cate())->where('id', $id)->field('id,fid,catename')->find()) {
                $cate[]=$wo;
            }
            if ($wo=(new \app\index\model\Cate())->where('id', $wo['fid'])->field('id,fid,catename')->find()) {
                $cate[]=$wo;
            }
        }
        return array_reverse($cate);
    }
    public function lit($id, $unm)
    {
        if (!$page=input('page')) {
            $page=1;
        }
        if (env('app_debug',false)) {
            $lit=(new \app\index\model\Article())->where('isopen', 1)->where('cateid', $id)->order('is_tui desc,create_time desc,id desc')->paginate($unm)->each(function ($item, $key) {
                if (!$item['pic']) {
                    $item['pic']=(new \app\index\model\ArticleImg())->where('aid', $item['id'])->value('pic');
                }
                return $item;
            });
        } else {
            if (!$lit=Cache::get('lit'.$id.$page)) {
                $lit=(new \app\index\model\Article())->where('isopen', 1)->where('cateid', $id)->order('is_tui desc,create_time desc,id desc')->paginate($unm)->each(function ($item, $key) {
                    if (!$item['pic']) {
                        $item['pic']=(new \app\index\model\ArticleImg())->where('aid', $item['id'])->value('pic');
                    }
                    return $item;
                });
                Cache::set('lit'.$id.$page, $lit, 3600);
            }
        }
        return $lit;
    }
    
    public function xia($idarr, $num, $offset, $order, $field, $where)
    {
        if (!is_array($idarr)) {
            return 'id 必须为数组！';
        }
        if (empty($idarr)) {
            $where2=true;
        } else {
            $where2=[
                 ['id', 'in', $idarr]
            ];
        }
        if ($order) {
            $order='is_tui desc,create_time desc,id asc';
        } else {
            $order='is_tui desc,create_time desc,id desc';
        }
        $download=Db::name('download')->where('isopen', 1)->where($where2)->where($where)->order($order)->limit($offset, $num)->field($field)->select()->toArray();
        return $download;
    }
}
